package cn.edu.scau.cmi.yangqing.hibernate.service;

import java.util.List;
import java.util.Set;

import org.hibernate.Transaction;

import cn.edu.scau.cmi.yangqing.hibernate.dao.TutorDao;
import cn.edu.scau.cmi.yangqing.hibernate.domain.Student;
import cn.edu.scau.cmi.yangqing.hibernate.domain.Tutor;
import cn.edu.scau.cmi.yangqing.hibernate.util.HibernateUtil;

public class TutorService {
	
	
	
	//添加导师
	public void addTutor(Tutor tutor) {
		Transaction beginTransaction = HibernateUtil.getCurrentSession().beginTransaction();
		TutorDao tutorDao = new TutorDao();
		
		try {
			tutorDao.addTutor(tutor);
		} catch (Exception e) {
			System.out.println("rollback?");
			beginTransaction.rollback();
		}
		System.out.println("commit?");
		beginTransaction.commit();
	}
	//删除导师
	public void deleteTutor(Integer id) {
		Transaction beginTransaction = HibernateUtil.getCurrentSession().beginTransaction();
		TutorDao tutorDao = new TutorDao();
		 try {
			 tutorDao.deleteTutor(id);
		} catch (Exception e) {
			beginTransaction.rollback();
		}
		 beginTransaction.commit();
	}
	public Tutor findTutorById(Integer id) {
		Transaction beginTransaction = HibernateUtil.getCurrentSession().beginTransaction();
		TutorDao tutorDao = new TutorDao();
		Tutor tutor =tutorDao.findTutorById(id);
		beginTransaction.commit();
		
		return tutor;
	}
	//修改导师
	public void updateTutor(Tutor tutor) {
		Transaction beginTransaction = HibernateUtil.getCurrentSession().beginTransaction();
		TutorDao tutorDao = new TutorDao();
		 try {
			 System.out.println(tutor);
			 tutorDao.updateTutor(tutor);
		} catch (Exception e) {
			System.out.println(e);
			beginTransaction.rollback();
		}
		 beginTransaction.commit();
	}
	//显示所有导师
	public List<Tutor> findAllTutor() {
		Transaction beginTransaction = HibernateUtil.getCurrentSession().beginTransaction();
		TutorDao tutorDao = new TutorDao();
		List<Tutor> list = tutorDao.findAllTutor();
		beginTransaction.commit();
		return list;
	}
	//通过导师id显示所教学生 
	public List<Student> teachStudentList(Integer id){
		Transaction beginTransaction = HibernateUtil.getCurrentSession().beginTransaction();
		TutorDao tutorDao = new TutorDao(); 
		 List<Student> list = tutorDao.teachStudentList(id);
		 beginTransaction.commit();
		 return list;
	}
	//通过导师id显示没有被教的学生 
		public List<Student> teachNoStudentList(Integer id){
			Transaction beginTransaction = HibernateUtil.getCurrentSession().beginTransaction();
			TutorDao tutorDao = new TutorDao(); 
			 List<Student> list = tutorDao.teachNoStudentList(id);
			 beginTransaction.commit();
			 return list;
		}
	//通过学生id,将其设置为授课
	public void teachStudent(Integer tutorid,Integer studentid) {
		Transaction beginTransaction = HibernateUtil.getCurrentSession().beginTransaction();
		TutorDao tutorDao = new TutorDao();
		try {
			tutorDao.teachStudent(tutorid,studentid);
		} catch (Exception e) {
			beginTransaction.rollback();
		}
		 beginTransaction.commit();
	}
	//通过学生id,将其设置为不授课
	public void teachNoStudent(Integer studentid) {
		Transaction beginTransaction = HibernateUtil.getCurrentSession().beginTransaction();
		TutorDao tutorDao = new TutorDao();
		try {
			tutorDao.teachNoStudent(studentid);
		} catch (Exception e) {
			beginTransaction.rollback();
		}
		 beginTransaction.commit();
	}
}
